{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import k3d\n",
    "import numpy as np\n",
    "from sklearn.neighbors import NearestNeighbors\n",
    "\n",
    "plot = k3d.plot()\n",
    "\n",
    "N = 10000\n",
    "vertices = np.random.normal(size=(N, 3)).astype(np.float32)\n",
    "\n",
    "nbrs = NearestNeighbors(n_neighbors=3, algorithm='ball_tree').fit(vertices)\n",
    "distances, indices = nbrs.kneighbors(vertices)\n",
    "\n",
    "obj = k3d.mesh(vertices, indices.astype(np.uint32), side='double')\n",
    "plot +=  obj\n",
    "\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "triangles_attribute = []\n",
    "\n",
    "for ind in indices:\n",
    "    v = vertices[ind]\n",
    "    triangles_attribute.append(np.linalg.norm(np.cross(v[2] - v[1], v[0] - v[1])) * 0.5)\n",
    "\n",
    "triangles_attribute = np.array(triangles_attribute, dtype=np.float32)\n",
    "\n",
    "obj.triangles_attribute = triangles_attribute\n",
    "obj.color_range = (np.max(triangles_attribute) / 8, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "nbTranslate": {
   "displayLangs": [
    "en",
    "pl"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "pl",
   "targetLang": "en",
   "useGoogleTranslate": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
